package main.java.io;

import scala.util.control.Exception;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.charset.StandardCharsets;

public class io5 {

    public  void send() throws IOException, InterruptedException {
        DatagramChannel dc = DatagramChannel.open();
        InetSocketAddress ad = new InetSocketAddress("127.0.0.1",9999);
        while (true){
            ByteBuffer bf = ByteBuffer.wrap("jsjsjsj".getBytes());
            dc.send(bf,ad);
            System.out.println("发送");
            Thread.sleep(1000);
        }

    }

    public void receive() throws IOException {
        DatagramChannel dc = DatagramChannel.open();
        InetSocketAddress ad = new InetSocketAddress(9999);
        dc.bind(ad);
        ByteBuffer bf = ByteBuffer.allocate(1024);
        while (true){
            bf.clear();
            SocketAddress sc = dc.receive(bf);
            bf.flip();

            System.out.println(sc.toString() + bf);
        }



    }
}
